// pages/ProductDetail/ProductDetail.js
const {
    Detail,
    GoShop,
    Collection,
    CollectionAdd,
    CollectionDel,
    CollectionList
} = require('../../utils/api')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        ArrInfo: [],
        Content: '',
        show: false,
        abc: 'a',
        Top1: 0,
        Top2: 509.5038146972656,
        Value: 1,
        ShopL: 0,
        Flag: false,
        CollDel: 0,
        CollList: [],

    },
    onPageScroll(e) {
        // console.log(e.scrollTop);
        if (e.scrollTop == 0) {
            this.setData({
                abc: 'a'
            })
        } else if (e.scrollTop >= 509.5038146972656) {
            this.setData({
                abc: 'b'
            })
        }
    },
    onClick(e) {
        console.log(e.detail);
        if (e.detail.index == 1) {
            wx.pageScrollTo({
                scrollTop: 509.5038146972656
            })

        } else if (e.detail.index == 2) {
            wx.pageScrollTo({
                scrollTop: 2708.800048828125
            })
        } else {
            wx.pageScrollTo({
                scrollTop: 0
            })
        }
    },
    onChange(e) {
        console.log(e.detail);
    },
    // 收藏
    ShouC(e) {
        let Flag = this.data.Flag
        let obj = e.currentTarget.dataset.obj
        let CollDel = this.data.CollDel
        let token = wx.getStorageSync('token')
        this.setData({
            Flag: !Flag
        })
        if (this.data.Flag == true) {
            CollectionAdd({
                goodsId: obj.id,
                type: obj.type,
                token: token
            }).then((res) => {
                console.log('加入收藏');
            })
            // 取消收藏
        } else {
            CollectionDel({
                goodsId: obj.id,
                type: obj.type,
                token: token,
                id: CollDel
            }).then((res) => {
                console.log('取消收藏了');
            })
        }
    },

    // 打开购物车弹框
    add(e) {
        this.setData({
            show: true
        })
        console.log(this.data.ArrInfo);
    },
    // 加入购物车
    adds() {
        GoShop({
            goodsId: this.data.ArrInfo.basicInfo.id,
            number: this.data.ArrInfo.basicInfo.minBuyNumber,
            token: wx.getStorageSync('token')
        }).then((res) => {
            console.log(res);
            wx.showToast({
                title: '成功加入购物车',
            })
            console.log(res);
            wx.setStorageSync('shop', res.items.length)
            // console.log();
            this.onClose()
        })
    },
    onClose() {
        this.setData({
            show: false
        })
    },
    // 数量加
    Jia(e) {
        // console.log(e);
        let val = e.currentTarget.dataset.val
        val.basicInfo.minBuyNumber++
        this.setData({
            ArrInfo: val
        })
        // console.log(this.data.ArrInfo);
    },
    // 数量减
    Jian(e) {
        // console.log(e);
        let val = e.currentTarget.dataset.val
        val.basicInfo.minBuyNumber--
        this.setData({
            ArrInfo: val
        })
        // console.log(this.data.ArrInfo);
    },
    // 跳转到购物车页面
    Jump() {
        wx.switchTab({
            url: '/pages/Shopping Cart/Shopping Cart',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        let Ids = options.id
        let type = options.type
        Detail({
            id: Ids
        }).then((res) => {
            // console.log(res);
            this.setData({
                ArrInfo: res,
                Content: res.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto"')
            })
            // console.log(this.data.ArrInfo);

        })
        // 检验收藏
        let obj = this.data.ArrInfo.basicInfo
        let token = wx.getStorageSync('token')

        Collection({
            token: token,
            goodsId: Ids,
            type: type
        }).then(res => {
            // console.log(res);
            let flag = false
            if (res == '已收藏') {
                flag = true
                console.log('已收藏');
            } else if (res.code == -1) {
                flag = false
                console.log('未收藏');
            }
            this.setData({
                Flag: flag
            })
        })

        // console.log(this.data.Flag);
        // console.log(flag);
        // 收藏列表
        CollectionList({
            token: token,
            goodsId: Ids,
            type: type
        }).then(res => {
            console.log(res);
            if (res.code == 700) {
                return
            } else {
                this.setData({
                    CollDel: res.result[0].id,
                    CollList: res.result[0],
                })
            }

            console.log(this.data.CollDel);
            console.log(this.data.CollList);
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // wx.getStorageSync('shop')

        this.setData({
            ShopL: wx.getStorageSync('shop')
        })
        // console.log(this.data.ShopL);

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})